package unitTest

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
)

type Monster struct {
	Name  string
	Age   int
	Skill string
}

func (this *Monster) Store(filePath string) bool {
	//先序列化
	data, err := json.Marshal(this)

	if err != nil {
		fmt.Println("marshal err:", err)
		return false
	}

	err = ioutil.WriteFile(filePath, data, 0777)
	if err != nil {
		fmt.Println("write file err:", err)
		return false
	}

	return true
}

func (this *Monster) ReStore(filePath string) bool {
	data, err := ioutil.ReadFile(filePath)
	if err != nil {
		fmt.Println("read file err:", err)
		return false
	}

	err = json.Unmarshal(data, this)
	if err != nil {
		fmt.Println("Unmarshal file err:", err)
		return false
	}
	fmt.Println("ReStore :", this)
	return true
}
